﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Cn.Edwon.Web;
using System.Text;
using Cn.Edwon.Web.Support;
using System.Security.Policy;
using Cn.Cellula.Client;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;

namespace Cn.Cellula.Website.Admin.Content.Fos {
    public partial class List : PageSupport {
        private String filePattern = "*.aspx|*.html|*.htm|*.txt|*.inc|*.ascx|*.config|*.xml|*.properties";
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                String filePath = Request.QueryString["f"];

                TreeNode node = new TreeNode();
                node.Text = ClientHelper.RootPath;
                node.SelectAction = TreeNodeSelectAction.None;
                node.Expanded = true;
                this.tv_Files.Nodes.Add(node);
                ListFile(ClientHelper.RootPath, "", node, filePath);

                if (!String.IsNullOrEmpty(filePath)) {
                    this.ph_Edit.Visible = true;
                    LoadContent(filePath);
                }
            }
        }

        private void ListFile(String rPath,String rPattern,TreeNode rNode,String rDefaultPath) {
            //目录
            String[] dirs = Directory.GetDirectories(rPath);
            DirectoryInfo di;
            String dname = "";
            foreach (String d in dirs) {
                di = new DirectoryInfo(d);
                dname = di.Name;
                if (dname.IndexOf(".") == 0
                    || dname.IndexOf("_") == 0
                    || dname.ToLower()=="app_code"
                    || dname.ToLower() == "obj") continue;
                TreeNode node = new TreeNode(dname+"/");
                node.Expanded = false;
                node.SelectAction = TreeNodeSelectAction.Expand;
                node.ImageUrl = "/cellula_client/controls/images/folder.gif";
                rNode.ChildNodes.Add(node);
                ListFile(d, rPattern, node, rDefaultPath);
            }
            String[] files = Directory.GetFiles(rPath);
            FileInfo fi;
            foreach (String f in files) {
                fi = new FileInfo(f);
                if (filePattern.IndexOf(fi.Extension) == -1) continue;
                if (fi.Name.IndexOf(".") == 0
                    || fi.Name.IndexOf("_") == 0) continue;
                TreeNode node = new TreeNode(fi.Name);
                node.NavigateUrl = "Admin/Content/Fos/List.aspx?f=" + HttpUtility.UrlEncode(f);
                node.ShowCheckBox = false;
                if (f == rDefaultPath) {
                    node.ImageUrl = "/cellula_client/controls/images/file_active.gif";
                    TreeNode _tn = rNode;
                    while (_tn != null) {
                        _tn.Expanded = true;
                        _tn = _tn.Parent;
                    }
                } else {
                    node.ImageUrl = "/cellula_client/controls/images/file.gif";
                }
                rNode.ChildNodes.Add(node);
            }
        }
        private void LoadContent(String rFilePath) {
            this.lab_FilePath.Text = rFilePath;
            if (!File.Exists(rFilePath)) {
                this.txt_Content.Text = "打开文件失败：文件不存在";
                this.btn_Save.Enabled = false;
                return;
            }
            try {
                this.txt_Content.Text = File.ReadAllText(rFilePath);
            } catch (Exception ex) {
                this.txt_Content.Text = "打开文件失败：" + ex.Message;
                this.btn_Save.Enabled = false;
                return;
            }
            this.btn_Save.Enabled = true;
        }

        protected void btn_Save_Click(object sender, EventArgs e) {
            try {
                File.Copy(this.lab_FilePath.Text, this.lab_FilePath.Text + ".bak", true);
                File.SetAttributes(this.lab_FilePath.Text, FileAttributes.Normal);
                File.WriteAllText(this.lab_FilePath.Text, this.txt_Content.Text, Encoding.UTF8);
                SiteInfo.Send(Page, TxtDef.SUCCEED, "保存成功");
                BizFactory.Get<ILogBiz>().Info("修改了文件:" + this.lab_FilePath.Text, this.Actor, Request);
            } catch (Exception ex) {
                BizFactory.Get<ILogBiz>().Error("修改文件(" + this.lab_FilePath.Text+")时出现错误", this.Actor, Request);
                this.txt_Content.Text = "保存失败：" + ex.Message;
            }
        }
    }
}
